package com.sojson.controller.bo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.base.impl.BaseController;
import com.sojson.config.exception.TransErrorCode;
import com.sojson.result.Result;
import com.sojson.service.VerifyCodeService;
import com.sojson.util.MatcheUtil;
import com.sojson.util.token.TokenUtil;
import com.sojson.util.verifycode.VerifyCodeUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 验证码控制器
 * 
 * @author liu
 * @Date 2020-06-15 10:44:11
 * @Description
 *
 */
@Api(tags = "验证码管理")
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController extends BaseController<Object> {

    @Autowired
    private VerifyCodeService verifyCodeServiceImpl;

    /**
     * 获取验证码
     * 
     * @param verifyCodePrefix 验证码前缀,用来区分不同验证码
     * @return
     * @throws Exception 
     */
    @GetMapping("/getVerifyCode.show")
    @ApiOperation(value = "获取字符串验证码", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "verifyCodePrefix", value = "验证码前缀,用来区分不同验证码")})
    public Object getVerifyCode(String verifyCodePrefix) throws Exception {
        String id = TokenUtil.getClaims(true).getId();
        return Result.success(verifyCodeServiceImpl
            .getVerifyCode(new StringBuilder(id).append(VerifyCodeUtil.STR).append(verifyCodePrefix).toString())
            .getCode());
    }

    /**
     * 获取邮箱验证码
     * 
     * @param verifyCodePrefix  验证码前缀,用来区分不同验证码
     * @param target            目标邮箱
     * @return
     * @throws Exception 
     */
    @GetMapping("/getVerifyCodeMail.show")
    @ApiOperation(value = "获取邮箱验证码")
    @ApiImplicitParams({
        //
        @ApiImplicitParam(name = "verifyCodePrefix", value = "验证码前缀,用来区分不同验证码"),
        //
        @ApiImplicitParam(name = "target", value = "目标邮箱", required = true)
        //
    })
    public Object getVerifyCodeMail(String verifyCodePrefix, String target) throws Exception {
        if (!MatcheUtil.isMail(target)) {
            return Result.fail("邮箱格式错误！");
        }
        String id = TokenUtil.getClaims(true).getId();
        verifyCodeServiceImpl.getVerifyCodeMail(
            new StringBuilder(id).append(VerifyCodeUtil.MAIL).append(verifyCodePrefix).toString(), target);
        return Result.success();
    }

    /**
     * 获取短信验证码
     * 
     * @param verifyCodePrefix  验证码前缀,用来区分不同验证码
     * @param tel               手机号
     * @return
     * @throws Exception 
     */
    @GetMapping("/getVerifyCodeSms.show")
    @ApiOperation(value = "获取短信验证码")
    @ApiImplicitParams({
        //
        @ApiImplicitParam(name = "verifyCodePrefix", value = "验证码前缀,用来区分不同验证码"),
        //
        @ApiImplicitParam(name = "tel", value = "手机号", required = true)
        //
    })
    public Object getVerifyCodeSms(String verifyCodePrefix, String tel) throws Exception {
        if (!MatcheUtil.isMobile(tel)) {
            return Result.fail("手机号格式错误！");
        }
        String id = TokenUtil.getClaims(true).getId();
        verifyCodeServiceImpl.getVerifyCodeSms(
            new StringBuilder(id).append(VerifyCodeUtil.SMS).append(verifyCodePrefix).toString(), tel);
        return Result.success();
    }

    /**
     * 获取验证码图片
     * 
     * @param verifyCodePrefix 验证码前缀,用来区分不同验证码
     * @return
     * @throws Exception
     */
    @GetMapping("/getVerifyCodeImage.show")
    @ApiOperation(value = "获取图片验证码")
    @ApiImplicitParams({@ApiImplicitParam(name = "verifyCodePrefix", value = "验证码前缀,用来区分不同验证码")})
    public void getVerifyCodeImage(String verifyCodePrefix) throws Exception {
        String id = TokenUtil.getClaims(true).getId();
        verifyCodeServiceImpl
            .getVerifyCodeImage(new StringBuilder(id).append(VerifyCodeUtil.IMG).append(verifyCodePrefix).toString());
    }

    /**
     * 获取验证码图片在本地目录的路径
     * 
     * @param verifyCodePrefix 验证码前缀,用来区分不同验证码
     * @return
     * @throws Exception
     */
    @GetMapping("/getVerifyCodeImageThisLocal.show")
    @ApiOperation(value = "获取验证码图片在本地目录的路径", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "verifyCodePrefix", value = "验证码前缀,用来区分不同验证码")})
    public Object getVerifyCodeImageThisLocal(String verifyCodePrefix) throws Exception {
        String id = TokenUtil.getClaims(true).getId();
        return Result.success(verifyCodeServiceImpl.getVerifyCodeImageThisLocal(
            new StringBuilder(id).append(VerifyCodeUtil.IMG).append(verifyCodePrefix).toString()));
    }

    /**
     * 对比验证码
     * 
     * @param verifyCodePrefix  验证码前缀
     * @param code              用户输入的值
     * @return 成功还是失败
     * @throws Exception 
     */
    @GetMapping("/verifyCode.show")
    @ApiOperation(value = "对比验证码")
    @ApiImplicitParams({
        //
        @ApiImplicitParam(name = "verifyCodePrefix", value = "验证码前缀,用来区分不同验证码"),
        //
        @ApiImplicitParam(name = "code", value = "用户输入的值", required = true)
        //
    })
    public Object verifyCode(String verifyCodePrefix, String code) throws Exception {
        String id = TokenUtil.getClaims(true).getId();
        if (!verifyCodeServiceImpl.verifyAndClearCode(id + verifyCodePrefix, code)) {
            return Result.fail(TransErrorCode.ERR_CODES);
        }
        return Result.success();
    }

}